package com.wgts.wgts.mvp.presenter.imp;

import com.wgts.wgts.mvp.model.IStoreActivityModel;
import com.wgts.wgts.mvp.presenter.IStoreActivityPresenter;
import com.wgts.wgts.mvp.view.IStoreActivityView;
import com.wgts.wgts.netmodle.HttpResultSubscriber;
import com.wgts.wgts.netmodle.req.UpDownProductRequest;
import com.wgts.wgts.netmodle.resp.ShopKeeperListEntity;
import com.wgts.wgts.netmodle.resp.StoreMsgEntity;

/*
 *
 *   data:2018/10/1119:47
 *
 */public class StoreActivityPresenter implements IStoreActivityPresenter {
     private IStoreActivityView view;
     private IStoreActivityModel model;
     private int pageIndex = 1;
     private int pageSize = 20;

    public StoreActivityPresenter(IStoreActivityView view, IStoreActivityModel model) {
        this.view = view;
        this.model = model;
    }

    @Override
    public void initList(Integer userId) {
        pageIndex = 1;
        model.initList(userId, pageIndex, pageSize, new HttpResultSubscriber<ShopKeeperListEntity>() {
            @Override
            public void onSuccess(ShopKeeperListEntity shopKeeperListEntity) {
                if (shopKeeperListEntity.list.size() < pageSize){
                    view.initList(shopKeeperListEntity, true);
                }else {
                    view.initList(shopKeeperListEntity, false);
                    pageIndex++;
                }
            }

            @Override
            public void _onError(int status, String msg) {
                view.onError(status, msg);
            }
        });
    }

    @Override
    public void loadMore(Integer userId) {
        model.initList(userId, pageIndex, pageSize, new HttpResultSubscriber<ShopKeeperListEntity>() {
            @Override
            public void onSuccess(ShopKeeperListEntity entity) {
                if (entity.list.size() < pageSize){
                    view.loadMore(entity, true);
                }else {
                    view.loadMore(entity, false);
                    pageIndex++;
                }
            }

            @Override
            public void _onError(int status, String msg) {

            }
        });
    }

    @Override
    public void storeMsg(Integer shopId) {
        model.storeMsg(shopId, new HttpResultSubscriber<StoreMsgEntity>() {
            @Override
            public void onSuccess(StoreMsgEntity storeMsgEntity) {
                view.storeMsg(storeMsgEntity);
            }

            @Override
            public void _onError(int status, String msg) {
                view.onError(status, msg);
            }
        });
    }

    @Override
    public void upDownProduct(String id, final int position) {
        UpDownProductRequest request = new UpDownProductRequest();
        request.productId = Integer.valueOf(id);
        request.updown = 0;
        model.updownProduct(request, new HttpResultSubscriber<Integer>() {
            @Override
            public void onSuccess(Integer integer) {
                view.upDownProductResult(position);
            }

            @Override
            public void _onError(int status, String msg) {
                view.onError(status, msg);
            }
        });
    }
}
